import React,{Component} from 'react';
import {Route, Redirect} from 'react-router-dom';
import qs from 'qs';

export default class Auth extends Component{

  state={
    err:1,//是否通过
    data:{}//授权通过后的数据
  };

  constructor(props){
    super(props);

    //授权条件 同步 localStorage
    //假定login时，返回的数据 有{err:0,msg:xx,data:{}}
    let user = window.localStorage.getItem('user');
    user = user ? qs.parse(user) : '';

    console.log('auth',user)

    // this.state.err = user.err;
    // this.state.data = user.data;

    this.state = {err:user.err,data:user.data}


  }

  render(){

    let {component:Component, ...rest} = this.props;

    return <Route {...rest} render={(props)=>(
        this.state.err === '0' ?
            <Component {...props} data={this.state.data} /> :
            <Redirect to="/login" />
    )}/>

  }

}